package com.iqiyi.pps.epg.core.utils;

import com.iqiyi.kiwi.message.Message;
import com.iqiyi.kiwi.message.MessageService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Qiyi
 * Date: 12-10-23
 * Time: 下午5:55
 * To change this template use File | Settings | File Templates.
 */
public class MessageUtils {
    private static final Logger logger = LoggerFactory.getLogger(MessageUtils.class);
    private static MessageService messageService = (MessageService) ApplicationContextUtil.getBean(MessageService.class);

    public static Long FirstDelayTime = 10000L;
    public static  int DelayTimeMultiple = 2;
    public static  int MaxResendTimes = 8;

    public static Long getMessageDelay(int processCount){
        switch (processCount){
            case 1:
                return 10*1000L;
            case 2:
                return 10*1000L;
            case 3:
                return 20*1000L;
            case 4:
                return 30*1000L;
            case 5:
                return 10*60*1000L;
            case 6:
                return 2*60*60*1000L;
            case 7:
                return 6*60*60*1000L;
            case 8:
                return 15*60*60*1000L;
            default:
                return 10000L;
        }
    }

    public static void sendRetryMessage(Message msg){
        Map<String, String> input = msg.getMap();
        int processCount = Integer.valueOf(input.get("processCount"));
        input.put("processCount", String.valueOf(processCount + 1));
        msg.setMap(input);
        msg.setDelay(MessageUtils.getMessageDelay(processCount + 1));
        messageService.send(msg);
        logger.info("[module:workflow][action:sendRetryMessage][step:finish][processCount:{}][message:{}]",processCount, JSONObject.fromObject(input).toString());
    }
}
